package com.jc.camerascan.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.MainThread;
import androidx.appcompat.app.AppCompatActivity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.jc.camerascan.R;
import com.jc.camerascan.core.BaseActivity;
import com.jc.camerascan.core.http.bean.CustomApiResult;
import com.jc.camerascan.core.http.callback.TipCallBack;
import com.jc.camerascan.entity.Banner;
import com.jc.camerascan.login.LoginActivity;
import com.jc.camerascan.utils.JurisdictionUtils;
import com.jc.camerascan.utils.MMKVUtils;
import com.jc.camerascan.utils.Utils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.StatusBarUtils;
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem;
import com.xuexiang.xui.widget.banner.widget.banner.base.BaseBanner;
import com.xuexiang.xutil.app.ActivityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.activity
 * @ClassName: NewSplashActivity
 * @Description: java类作用描述
 * @Author: XuChi
 * @CreateDate: 2020/8/24 19:53
 * @Version: 1.0
 */
public class NewSplashActivity extends BaseActivity implements
        View.OnClickListener {


    @BindView(R.id.sp_jump_btn)
    Button mSpJumpBtn;


    @BindView(R.id.splash_container)
    FrameLayout container;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    public void initView() {
        StatusBarUtils.translucent(this);
        ButterKnife.bind(this);
//        showAndDownSplash();
        mSpJumpBtn.setOnClickListener(this);
//        spBg.setOnClickListener(this);
//        countDownTimer.start();
        XHttp.get("/api/Home/IsOpenAdvert")
                .execute(new CallBackProxy<CustomApiResult<Integer>, Integer>(new TipCallBack<Integer>() {
                    @Override
                    public void onSuccess(Integer response) throws Throwable {
                        if (response != null && response == 1) {
                            fetchSplashAD();
                        } else {
                            goToNext();
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        goToNext();
                    }
                }) {
                });
    }

    private TTAdNative mTTAdNative;

    private void fetchSplashAD() {
        AdSlot adSlot = null;
        adSlot = new AdSlot.Builder()
                .setCodeId("887370245")
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                .build();
        mTTAdNative = TTAdSdk.getAdManager().createAdNative(this);
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            @MainThread
            public void onError(int code, String message) {
//                mSpJumpBtn.setVisibility(View.VISIBLE);
//                spBg.setVisibility(View.VISIBLE);
//                countDownTimer.start();
                goToNext();
            }

            @Override
            @MainThread
            public void onTimeout() {
                goToNext();
//                mSpJumpBtn.setVisibility(View.VISIBLE);
//                spBg.setVisibility(View.VISIBLE);
//                countDownTimer.start();
            }

            @Override
            @MainThread
            public void onSplashAdLoad(TTSplashAd ad) {
//                Log.d(TAG, "开屏广告请求成功");
//                mHasLoaded = true;
//                mHandler.removeCallbacksAndMessages(null);
//                if (ad == null) {
//                    return;
//                }
                //获取SplashView
                View view = ad.getSplashView();

                container.removeAllViews();
                //把SplashView 添加到ViewGroup中
                container.addView(view);
                //设置SplashView的交互监听器
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int type) {

                    }

                    @Override
                    public void onAdShow(View view, int type) {

                    }

                    @Override
                    public void onAdSkip() {
                        goToNext();
                    }

                    @Override
                    public void onAdTimeOver() {
                        goToNext();
                    }
                });
            }
        }, 5000);

//        fetchSplashADTime = System.currentTimeMillis();
//        splashAD = new SplashAD(activity, appId, posId, adListener, fetchDelay);
//        splashAD.fetchAndShowIn(adContainer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null)
            countDownTimer.cancel();
    }

    //由于CountDownTimer有一定的延迟，所以这里设置3400
    private CountDownTimer countDownTimer = new CountDownTimer(3400, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mSpJumpBtn.setText("跳过(" + millisUntilFinished / 1000 + "s)");
        }

        @Override
        public void onFinish() {
            mSpJumpBtn.setText("跳过(" + 0 + "s)");
            goToNext();
        }
    };

    @Override
    public void onClick(View view) {
        goToNext();
    }

    private void goToNext() {
//        Intent intent = new Intent();
//        intent.setClass(SplashActivity.this, NewMainActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//        countDownTimer.cancel();
//        finish();
        if (JurisdictionUtils.isAgree()) {
//            ActivityUtils.startActivity(MainActivity.class);
            if (JurisdictionUtils.isLogin()) {
                ActivityUtils.startActivity(MainActivity.class);
            } else {
                ActivityUtils.startActivity(LoginActivity.class);
            }
            finish();
        } else {
            Utils.showPrivacyDialog(this, (dialog, which) -> {
                dialog.dismiss();
                MMKVUtils.put("key_agree_privacy", true);
//                ActivityUtils.startActivity(MainActivity.class);
                ActivityUtils.startActivity(LoginActivity.class);
                finish();
            });
        }
    }

}

